// 初始化PanelOpt对象
var orgAuthorityMgrPanelOpt = {
    id:"orgAuthorityMgrPanel",
    bombType:BOMB_TYPE2
};

// 机构区域权限编辑提交
function toSubOrgSite() {
    var orgId = $("#deptAuthorityMgr_deptId").val();
    var siteTreeObj = $.fn.zTree.getZTreeObj("siteSel_tree");
    var checkedSiteNodes = siteTreeObj.getCheckedNodes();
    var siteIDs = "";

    var j = 0;//选择不是隧道信息累计次数
    if(checkedSiteNodes){
        $(checkedSiteNodes).each(function(i,obj){
            if (obj.nodeType == "site") {
                if(i+1==checkedSiteNodes.length){
                    siteIDs += obj.realID;
                    return;
                }
                siteIDs += obj.realID+",";
            } else {
                j++;
            }
        });
    }

    if (siteIDs == null || siteIDs == "") {
        // $.wdfProgress.progress("请稍等...",$("body"));
        $.wdfProgress.showMsg("隧道信息为空,请正确选择隧道信息！",$("body"),true);
        setTimeout(function () {
            $.wdfProgress.progRemove($("body"));
        },1000);
        return;
    }

    $.wdfProgress.progress("请稍等...",$("body"));

    $.ajax({
        url:basePath+"/department/updateSite",
        data:{
            orgId:orgId,
            siteIDs:siteIDs,
            authObjType:4, //用户业务权限域类型:1区域,2线路,3路段,4场地
            cType:"PCWEB"
        },
        dataType:"json",
        type: "post",
        contentType:"application/x-www-form-urlencoded",
        beforeSend:null,
        success:function(data){
            if(data.status==1){
                $.wdfProgress.showMsg(data.msg,$("body"),true);

                $.wdfProgress.progRemove($("body"));
                orgAuthorityMgrPanel.close();

                departmentGridOpt.keepPage=true;
                $("#departmentGrid").wdfGrid("reload",departmentGridOpt);

            }
        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
        }
    });
}

// 机构下拉树初始化
// 注:如果enable设置为true,请务必设置setting.data.simpleData内的其他参数: idKey/pIdKey/rootPId,并且让数据满足父子关系
var siteZTreeSetting = {
    data : {
        simpleData : {
            enable : true,
            idKey: "id",
            pIdKey: "pId",
            rootPId: 0
        }
    },
    check: {
        enable: true,
        chkStyle:"checkbox",
        chkboxType: { "Y": "", "N": "" }
    },
    callback : {
        // onCheck:orgOnCheck, // 机构与角色无任何关联
        onClick: function (e, treeId, treeNode, clickFlag) {
            var siteTree =  $.fn.zTree.getZTreeObj("siteSel_tree");
            siteTree.checkNode(treeNode, !treeNode.checked, true);
        }
    }
};

// 机构隧道全选事件
function checkAuthorityAllSite(e) {
    var siteTree =  $.fn.zTree.getZTreeObj("siteSel_tree");
    if($(e).hasClass("uncheckBtn")){
        $(e).removeClass("uncheckBtn");
        $(e).addClass("checkedBtn");
        $(e).prev("input[type=checkbox]").prop("checked",true);
        siteTree.checkAllNodes(true);
    }else{
        $(e).removeClass("checkedBtn");
        $(e).addClass("uncheckBtn");
        $(e).prev("input[type=checkbox]").prop("checked",false);
        siteTree.checkAllNodes(false);
    }
}

// 机构隧道权限设置
function orgSiteAuthority(id, name) {
    $("#userAuthorityMgr_tit").find(".userAuthorityMgr_tab").eq(0).addClass("indexAuthTab");
    $("#checkAuthorityAllOrgBtn").attr("class","checkAllBtn uncheckBtn");
    $("#siteAllCheck").prop("checked",false);
    $("#user_systemAuthority_box").show();
    $("#deptAuthorityMgr_deptId").val(id);

    var rowDatas=$("#departmentGrid").wdfGrid("getRowDatasByDK",{key:"id",value:id});
    var dept = rowDatas[0];
    orgAuthorityMgrPanel.find("#departmentName_addform").val(name);
    orgAuthorityMgrPanel.find("#dept_level_addform").val(getOrgLevelName(dept.level));
    orgAuthorityMgrPanel.find("#dept_description_addform").val(dept.description);
    orgAuthorityMgrPanel.find("#parent_department_addform").val(dept.pName);
    orgAuthorityMgrPanel.show("机构隧道设置"+"("+name+")");

    var siteIDsArray=[];

    $.wdfProgress.progress("请稍等...",$("body"));
    $.ajax({
        // 远程调用隧道信息接口
        url:basePath+"/department/listBaseDatasForTree",
        data:{
            listBaseDatasForTree:1
        },
        dataType:"json",
        type: "post",
        contentType:"application/x-www-form-urlencoded",
        beforeSend:null,
        success:function(data){
            /**获取所有节点*/
            data = data.data;
            // 先设置节点复选框属性值,再进行下拉树初始化
            $(data.data).each(function(i,obj){
                //过滤数据，如果不为site则不让其展示checkbox,不让其选择
                if(obj.nodeType != 'site'){
                    //z-tree数据结构属性
                    obj["nocheck"] = true;
                    // obj.nocheck = true;
                }
            });
            $.fn.zTree.init($("#siteSel_tree"),siteZTreeSetting,data.data);
            var siteTree =  $.fn.zTree.getZTreeObj("siteSel_tree");
            siteTree.checkAllNodes(false);
            // 默认扩展下拉树结构
            siteTree.expandAll(true);//默认展开 yuanp 20191127

            // 根据机构id查询隧道信息
            $.ajax({
                url:basePath+"/department/getOrgSiteTree",
                data:{
                    orgId:id
                },
                async:false,
                dataType:"json",
                type: "post",
                contentType:"application/x-www-form-urlencoded",
                beforeSend:null,
                success:function(data){
                    rowData.siteIDs = data.data;
                },
                error:function (XMLHttpRequest, textStatus, errorThrown) {
                    $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
                }
            });

            if(rowData.siteIDs != null){
                if(rowData.siteIDs.length > 0){
                    $(rowData.siteIDs).each(function (i, obj) {
                        siteIDsArray.push(obj.siteId);
                    });
                }
            }
            for (var i=0;i<siteIDsArray.length;i++) {
                var checkNode = siteTree.getNodeByParam("realID",siteIDsArray[i], null);
                if (checkNode != null) {
                    siteTree.checkNode(checkNode,true,true);
                }
            }

            $.wdfProgress.showMsg("数据加载成功！",$("body"),true);
            $.wdfProgress.progRemove($("body"));
        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
        }
    });

}

$(function () {
    // 初始化机构、隧道权限编辑页面
    orgAuthorityMgrPanel=$("#orgAuthorityMgrPanel").wdfPanel("FormPanel",orgAuthorityMgrPanelOpt);

})